<template>
	<view class="content">
		订单
		<button type="primary" @click="startScreenRecordWithFile()">开始录屏</button>
		<button type="primary" @click="stopScreenRecord()">停止录屏</button>
		<button type="primary" @click="takeScreenShotWithFile()">截屏</button>
		<button type="primary" @click="openAntiRecordScreen()">开启防录屏截屏</button>
		<button type="primary" @click="closeAntiRecordScreen()">关闭防录屏截屏</button>
		
	</view>
</template>

<script>
	
	//插件对象
	var Screen = uni.requireNativePlugin('Yam-Screen');
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
			//检查权限并授权
			Screen.checkPermission(function(res){
				console.log(res);
			});
			
			if (uni.getSystemInfoSync().platform == 'android') {
				//打开相册、图片预览或其他原生界面时开启防录屏，仅android支持
				Screen.setAntiActivityList([
					"io.dcloud.feature.nativeObj.photoview.PhotoActivity",
					//"io.dcloud.WebAppActivity",
					//"io.dcloud.ProcessMediator",
					//"io.dcloud.WebviewActivity",
					"com.dmcbig.mediapicker.PickerActivity",
					"com.dmcbig.mediapicker.PreviewActivity",
					"io.dcloud.feature.gallery.imageedit.IMGEditActivity"//,
					//"io.dcloud.sdk.activity.WebViewActivity",
					//"com.taobao.weex.devtools.toolbox.PerformanceActivity",
					//"com.blankj.utilcode.util.PermissionUtils$PermissionActivity"
				]);
				
				//设置录屏通知配置，仅android支持
				Screen.setRecordConfig({
					contentTitle:"默认app名称",
					contentText:"正在录屏"
				});
			}
			
			if (uni.getSystemInfoSync().platform == 'ios') {
				//判断是否在屏幕录制
				var ret = Screen.isCaptured();//true:正在录制 false：停止录制
				
				//监听截屏录屏回调
				Screen.setScreenCallback(function(res){
				    console.log(res);
				    if (res.type == "onScreenShot") {
				        //监听截屏回调事件
						uni.showModal({
							content:"onScreenShot"
						})
				    } else if (res.type == "onScreenCapturedDidChange") {
				        //监听屏幕录制回调事件
				        var isCaptured = res.data;//true:正在录制 false：停止录制
				    }
				});
			}
			
			//
			setTimeout(()=>{
				console.log(111)
				this.openAntiRecordScreen();
				console.log(222)
			},100)
			
		},
		methods: {
			startScreenRecordWithFile() {
				//开始录屏
				var videoPath = plus.io.convertLocalFileSystemURL('_doc/screen_capture_video.mp4');
				Screen.setAudioEnabled(true);//true:开启音频 false:关闭音频
				Screen.startScreenRecordWithFile(videoPath, function(res){
					console.log(res);
					if (res.type == "onSuccess") {
						//录屏成功，保存视频到相册
						var path = res.data;
						Screen.saveVideoToAlbum(path);
					} else if (res.type == "onFailed") {
						//失败
					}
				});
			},
			stopScreenRecord() {
				//停止录屏
				Screen.stopScreenRecord();
			},
			takeScreenShotWithFile() {
				//截屏
				var imagePath = plus.io.convertLocalFileSystemURL("_doc/screen_shot_img.jpg");
				Screen.takeScreenShotWithFile(imagePath, function(res){
					console.log(res);
					if (res.type == "onSuccess") {
						//截屏成功，保存图片到相册
						var path = res.data;
						Screen.saveImageToAlbum(path);
					} else if (res.type == "onFailed") {
						//失败
					}
				});
			},
			openAntiRecordScreen() {
				//开启防录屏截屏
				Screen.openAntiRecordScreen();
			},
			closeAntiRecordScreen() {
				//关闭防录屏截屏
				Screen.closeAntiRecordScreen();
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
